package org.logan.sample.web.base.shiro;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.apache.shiro.util.CollectionUtils;
import org.logan.sample.base.cache.redis.RedisCacheManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Desc：
 * 
 * @Title：redisCache
 * @author: Logan
 * @version: 1.0
 * @date: 2016年11月28日 下午2:29:33
 */
public class RedisCache<K, V> implements Cache<K, V> {
	protected final Logger logger = LoggerFactory.getLogger(getClass());

	private String keyPrefix = null;

	public RedisCache(String name) {
		// set the prefix
		this.keyPrefix = name;
		try {
			RedisCacheManager.createCache(name,0);
		} catch (Exception e) {
			logger.error("创建缓存异常：", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public V get(K key) throws CacheException {
		return (V) RedisCacheManager.getRedisCacheWrapperByName(keyPrefix).getObject(key.toString());
	}

	@SuppressWarnings("unchecked")
	@Override
	public V put(K key, V value) throws CacheException {
		RedisCacheManager.getRedisCacheWrapperByName(keyPrefix).setObject(key.toString(), value);
		return (V) RedisCacheManager.getRedisCacheWrapperByName(keyPrefix).getObject(key.toString());

	}

	@Override
	public V remove(K key) throws CacheException {
		@SuppressWarnings("unchecked")
		V value = (V) RedisCacheManager.getRedisCacheWrapperByName(keyPrefix).getObject(key.toString());
		RedisCacheManager.getRedisCacheWrapperByName(keyPrefix).removeObject(key.toString());
		return value;

	}

	@Override
	public void clear() throws CacheException {
		RedisCacheManager.getRedisCacheWrapperByName(keyPrefix).removeAll();

	}

	@Override
	public int size() {
		Long longSize = RedisCacheManager.getRedisCacheWrapperByName(keyPrefix).getJedis().dbSize();
		return longSize.intValue();

	}

	@SuppressWarnings("unchecked")
	@Override
	public Set<K> keys() {
		try {
			Set<String> keys = RedisCacheManager.getRedisCacheWrapperByName(keyPrefix).getJedis()
					.keys(this.keyPrefix + "*");
			if (CollectionUtils.isEmpty(keys)) {
				return Collections.emptySet();
			} else {
				Set<K> newKeys = new HashSet<K>(keys.size());
				for (String key : keys) {
					newKeys.add((K) key);
				}
				return newKeys;
			}
		} catch (Throwable t) {
			throw new CacheException(t);
		}

	}

	@Override
	public Collection<V> values() {
		try {
			Set<String> keys = RedisCacheManager.getRedisCacheWrapperByName(keyPrefix).getJedis()
					.keys(this.keyPrefix + "*");
			if (!CollectionUtils.isEmpty(keys)) {
				List<V> values = new ArrayList<V>(keys.size());
				for (String key : keys) {
					@SuppressWarnings("unchecked")
					V value = get((K) key);
					if (value != null) {
						values.add(value);
					}
				}
				return Collections.unmodifiableList(values);
			} else {
				return Collections.emptyList();
			}
		} catch (Throwable t) {
			throw new CacheException(t);
		}

	}

}
